package com.cb.service.impl;

import com.alibaba.fastjson.JSON;
import com.cb.dao.ConductorFilmDao;
import com.cb.pojo.Film;
import com.cb.pojo.FilmScore;
import com.cb.pojo.ScreeninFormation;
import com.cb.service.ConductorFilmService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class ConductorFilmServiceImpl implements ConductorFilmService {

    @Autowired
    private ConductorFilmDao conductorfilmdao;

    /**
     * 查询影院内全部电影
     * */
    @Override
    public String getAllFilm(HashMap map) {
        String yyid = String.valueOf(map.get("yyid"));
        int pageNum = Integer.parseInt(String.valueOf(map.get("page")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("size")));
        PageHelper.startPage(pageNum, pageSize);
        List<Film> films = conductorfilmdao.selectAllFilm(yyid);
        List<FilmScore> scores = conductorfilmdao.getScore();
        int i = 0;
        for (Film film:films){
            for (FilmScore score:scores){
                if (film.getId().equals(score.getYpid())){
                    Film change = films.get(i);
                    change.setScore(score.getScore());
                    break;
                }
            }
            i++;
        }
        //List<Film> films = conductorfilmdao.selectAllFilm(yyid,page,size);
        String filmsJson = JSON.toJSONString(films);
        return filmsJson;
    }

    /**
     * 根据条件筛选电影
     * */
    @Override
    public String getFilm(HashMap map) {
        int pageNum = Integer.parseInt(String.valueOf(map.get("page")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("size")));
        PageHelper.startPage(pageNum, pageSize);
        List<Film> films = conductorfilmdao.getFilm(map);
        List<FilmScore> scores = conductorfilmdao.getScore();
        int i = 0;
        for (Film film:films){
            for (FilmScore score:scores){
                if (film.getId().equals(score.getYpid())){
                    Film change = films.get(i);
                    change.setScore(score.getScore());
                    break;
                }
            }
            i++;
        }
        String filmsJson = JSON.toJSONString(films);
        return filmsJson;
    }

    /**
     * 根据搜索框模糊查询
     * */
    @Override
    public String searchFilm(HashMap map) {
        String yyid = String.valueOf(map.get("yyid"));
        String keyword = String.valueOf(map.get("keyword"));
        int pageNum = Integer.parseInt(String.valueOf(map.get("page")));
        int pageSize = Integer.parseInt(String.valueOf(map.get("size")));
        if (keyword.equals("") || keyword==null){
            PageHelper.startPage(pageNum, pageSize);
            List<Film> films = conductorfilmdao.selectAllFilm(yyid);
            List<FilmScore> scores = conductorfilmdao.getScore();
            int i = 0;
            for (Film film:films){
                for (FilmScore score:scores){
                    if (film.getId().equals(score.getYpid())){
                        Film change = films.get(i);
                        change.setScore(score.getScore());
                        break;
                    }
                }
                i++;
            }
            String filmsJson = JSON.toJSONString(films);
            return filmsJson;
        }
        else {
            List<Film> films = conductorfilmdao.searchFilm(map);
            List<FilmScore> scores = conductorfilmdao.getScore();
            int i = 0;
            for (Film film:films){
                for (FilmScore score:scores){
                    if (film.getId().equals(score.getYpid())){
                        Film change = films.get(i);
                        change.setScore(score.getScore());
                        break;
                    }
                }
                i++;
            }
            String filmsJson = JSON.toJSONString(films);
            return filmsJson;
        }
    }

    /**
     * 列出后一周在上映的电影
     * */
    @Override
    public String getNextWeekFilm(HashMap map) {
        String yyid = String.valueOf(map.get("yyid"));
        List<Film> films = conductorfilmdao.getNextWeekFilm(yyid);
        List<FilmScore> scores = conductorfilmdao.getScore();
        int i = 0;
        for (Film film:films){
            for (FilmScore score:scores){
                if (film.getId().equals(score.getYpid())){
                    Film change = films.get(i);
                    change.setScore(score.getScore());
                    break;
                }
            }
            i++;
        }
        String filmsJson = JSON.toJSONString(films);
        return filmsJson;
    }

    /**
     * 售票员通过日期查询某个电影的放映安排
     * 得到影院内某个电影在某一天中的全部场次(选择电影ypid，选择日期date,影院编号yyid)
     * */
    @Override
    public String getFilmPlan(HashMap map) {
        List<ScreeninFormation> sc = conductorfilmdao.getFilmPlan(map);
        String scJson = JSON.toJSONString(sc);
        return scJson;
    }

}
